package com.jeesite.modules.dataScreen.service;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeesite.modules.dataScreen.dao.EventConductDao;
import com.jeesite.modules.dataScreen.utils.MyDateUtils;
import com.jeesite.modules.dataScreen.web.vo.TrafficDTO;
import com.jeesite.modules.dataScreen.web.vo.event.EventConductCellVo;
import com.jeesite.modules.dataScreen.web.vo.event.EventConductItemVo;
import com.jeesite.modules.dataScreen.web.vo.event.EventConductVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class EventConductService {

    @Resource
    private EventConductDao eventConductDao;


    /**
     * 事件处置-右侧列表
     *
     * @return
     */
    public IPage<EventConductVo> eventList(Page<EventConductVo> page, String keywords) {
        DateTime[] times = MyDateUtils.lately(keywords);
        IPage<EventConductVo> eventConductVoIPage = eventConductDao.eventList(page, times);
        return eventConductVoIPage;
//        if (!eventConductVoIPage.getRecords().isEmpty()) {
//            return eventConductVoIPage;
//        }
//        Page<EventConductVo> nPage = new Page<>(1, 5);
//        return eventConductDao.windowList(nPage, null, "status1", null, null, null, null, null);

    }

    /**
     * 事件处置-左侧接口
     *
     * @return
     */
    public EventConductCellVo cellItem() {
        EventConductCellVo result = new EventConductCellVo();
        int year = LocalDate.now().getYear();
        List<EventConductItemVo> list = eventConductDao.cellItem(year);
        result.setBreakIngCount(list.stream().mapToInt(EventConductItemVo::getBreakIngCount).sum());
        result.setBreakCloseCount(list.stream().mapToInt(EventConductItemVo::getBreakCloseCount).sum());
        result.setBreakCount(result.getBreakIngCount() + result.getBreakCloseCount());
        result.setEvents(list);
        return result;
    }

    /**
     * 事件处置-弹窗列表
     *
     * @return
     */
    public IPage<EventConductVo> windowList(Page<EventConductVo> page, String mainType, String status,
                                            String name,String start, String end,String level,String keywords) {

        DateTime[] times = MyDateUtils.lately(keywords);
        return eventConductDao.windowList(page, mainType, status,name,start,end,level,times);
    }

    public List<EventConductVo> incidentNumber(String keywords) {

        DateTime[] times = MyDateUtils.lately(keywords);
        return eventConductDao.incidentNumber(times);
    }


    /**
     * 事件详情弹窗
     *
     * @return
     */
    public List<TrafficDTO> eventDetails(int oid,String sjyptid) {

        List<TrafficDTO> trafficDTOAll = eventConductDao.eventDetails(oid,sjyptid);

        if (!trafficDTOAll.isEmpty()) {
            trafficDTOAll.get(0).setStatus("发生");
        }

        Optional<TrafficDTO> lastCompleted = trafficDTOAll.stream()
                .filter(dto -> "完结".equals(dto.getStatus()))
                .sorted(Comparator.comparing(TrafficDTO::getHappenTime).reversed())
                .findFirst();

        Map<String, TrafficDTO> describeMap = new LinkedHashMap<>();

        for (TrafficDTO dto : trafficDTOAll) {
            describeMap.putIfAbsent(dto.getDescribe(), dto);
        }

        List<TrafficDTO> result = new ArrayList<>(describeMap.values());


        result.removeIf(dto -> "完结".equals(dto.getStatus()));

        lastCompleted.ifPresent(result::add);
        List<TrafficDTO> resultList = result.stream()
                .peek(trafficDTO -> trafficDTO.setDescribe(Optional.ofNullable(trafficDTO.getDescribe())
                        .map(describe -> describe.replaceAll("<br>", ""))
                        .orElse(null)))
                .peek(trafficDTO -> trafficDTO.setPlace(Optional.ofNullable(trafficDTO.getPlace())
                        .map(place -> place.replaceAll("\\(null\\)", ""))
                        .map(place -> place.split(" ")[0])
                        .orElse(null)))
                .collect(Collectors.toList());


        return resultList;
    }


    /**
     * 事件等级数量
     * @return
     */
    public List<JSONObject> levelCount(int status,String keywords) {
        DateTime[] times = MyDateUtils.lately(keywords);
       return eventConductDao.levelCount(status,times);
    }

    /**
     * 事件信息
     * @return
     */
    public List<JSONObject> eventInformation(String oid) {
        return eventConductDao.eventInformation(oid);
    }
}
